<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\Plugin\ContactBatchAdd\Logic;

use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use MoChat\App\Utils\Url;
use MoChat\App\WorkAgent\Contract\WorkAgentContract;
use MoChat\App\WorkAgent\QueueService\MessageRemind;
use MoChat\App\WorkEmployee\Contract\WorkEmployeeContract;
use MoChat\Plugin\ContactBatchAdd\Contract\ContactBatchAddImportContract;

/**
 * 导入客户-未添加客户通知员工.
 *
 * Class NoticeEmployeeLogic
 */
class NoticeEmployeeLogic
{
    /**
     * @Inject
     * @var ContactBatchAddImportContract
     */
    protected $contactBatchAddImportService;

    /**
     * @Inject
     * @var WorkEmployeeContract
     */
    protected $workEmployeeService;

    /**
     * @Inject
     * @var WorkAgentContract
     */
    protected $workAgentService;

    /**
     * @Inject
     * @var StdoutLoggerInterface
     */
    private $logger;

    /**
     * @param array $params 请求参数
     */
    public function handle(array $params): array
    {
        # # 获取待添加客户
        $result = $this->handleContact($params);
        if (count($result)) {
            $this->handleNotice($result);
        }
        return [];
    }

    /**
     * @param int $params 参数
     */
    private function handleContact(array $params): array
    {
        $corpId = $params['corpId'];

        return $this->contactBatchAddImportService->getContactBatchAddImportOptionWhereGroup([
            ['corp_id', '=', $corpId],
            ['status', '=', 1],
            ['upload_at', '<=', date('Y-m-d 23:59:59', time() - $params['undoneTimeOut'] * 86400)],
        ], ['employee_id'], [
            'corp_id', 'record_id', 'employee_id', Db::raw('count(1) as num'),
        ]);
    }

    private function handleNotice(array $group): array
    {
        // TODO 通知
        foreach ($group as $item) {
            $agent = $this->workAgentService->getWorkAgentRemindByCorpId($item['corpId'], ['id']);
            if (empty($agent)) {
                $this->logger->error('批最加好友-任务提醒-未配置自建应用信息，无法发送提醒！');
            }
            $employee = $this->workEmployeeService->getWorkEmployeeById((int) $item['employeeId']);
            if (empty($employee)) {
                $this->logger->error('批最加好友-任务提醒-员工信息不存在');
            }
            $url = Url::getSidebarBaseUrl() . '/contactBatchAdd?agentId=' . $agent['id'] . '&batchId=' . $item['recordId'];
            $text = "【管理员提醒】您有客户未添加哦！\n" .
                "提醒事项：添加客户\n" .
                "客户数量：{$item['num']}名\n" .
                "记得及时添加哦\n" .
                "<a href='{$url}'>点击查看详情</a>";
            $messageRemind = make(MessageRemind::class);
            $messageRemind->sendToEmployee(
                (int) $employee['corpId'],
                $employee['wxUserId'],
                'text',
                $text
            );
        }
        return [];
    }
}
